package org.acompanhamento.pages.task;

import java.util.ArrayList;
import java.util.List;

import jmine.tec.di.annotation.Injected;
import jmine.tec.web.wicket.component.autocomplete.NaturalKeyAutocomplete;
import jmine.tec.web.wicket.pages.form.FormPage;
import jmine.tec.web.wicket.pages.form.FormType;

import org.acompanhamento.HelpMessages;
import org.acompanhamento.dao.SprintDAO;
import org.acompanhamento.domain.Member;
import org.acompanhamento.domain.Task;
import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import bancosys.tec.exception.MessageCreator;
import bancosys.tec.security.annotation.Secure;
import bancosys.tec.security.authorization.UrlPermission;
import bancosys.tec.utils.date.Date;

/**
 * 
 * @author icaro.kume
 *
 */
@Secure(id = "GENERIC_TEAM_MEMBER_URL", permissionType = UrlPermission.class)
public class EditTask extends FormPage<Task>{

	@Injected
	private SprintDAO sprintDAO;

	public EditTask(PageParameters pageParameters) {
		super(pageParameters);
	}

	public EditTask(PageParameters pageParameters, Page pageInstance) {
		super(pageParameters, pageInstance);
	}

	public EditTask(Page sourcePage, PageParameters sourcePageParameters,
			Task entity, FormType formType) {
		super(sourcePage, sourcePageParameters, entity, formType);
	}

	@Override
	protected Task createEntity() {
		Task task = super.createEntity();
		task.setRecordDate(new Date());
		return task;
	}

	@Override
	protected List<Component> createFormComponents() {
		List<Component> form = new ArrayList<Component>();

		// TODO: gerar automaticamente?
		form.add(this.createTextField("identifier").setRequired(true));

		form.add(this.createTextField("summary").setRequired(true));
		form.add(new TextArea<>("description").setRequired(true));
		form.add(this.createTextField("points").setRequired(true));
		form.add(this.createDropDown("sprint", this.sprintDAO.findAll()));
		form.add(new NaturalKeyAutocomplete<Member>(Member.class, "responsible"));

		form.add(this.createDateField("recordDate", true));
		return form;
	}

	@Override
	protected boolean beforeSave(Task target) {
		boolean save = super.beforeSave(target);
		return save;
	}

	@Override
	protected MessageCreator getHelpTextCreator() {
		return HelpMessages.TASK_EDIT;
	}
}
